package com.oa.modules.approvalsec1.timer;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.DateUtils;
import com.oa.modules.approvalsec1.entity.*;
import com.oa.modules.approvalsec1.service.*;
import com.oa.modules.sys.entity.SysUserEntity;
import com.oa.modules.sys.service.SysUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Log4j2
public class TimerController {

    @Autowired
    private AssistInManagingService assistInManagingService;

    @Autowired
    private ToDoImmediatelyService toDoImmediatelyService;

    @Autowired
    private NewsNoticeService newsNoticeService;

    @Autowired
    private SuperviseHandleService superviseHandleService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserAssignService userAssignService;

    @Autowired
    private RoomStoreService roomStoreService;

    @Autowired
    private BiddProjectService biddProjectService;

    @Autowired
    private BiddService biddService;


    @Scheduled(cron = "0 0 */1 * * ?")
    @Async
    public void cron() {
        System.err.println("开始查找24小时未办理完成的");
        Date newDate = new Date();
        List<AssistInManagingEntity> assistList = assistInManagingService.queryIsAssign();
        for (int i = 0; i < assistList.size(); i++) {
            Date assignDate = assistList.get(i).getAssignDate();
            if (DateUtils.getAfterDay(assignDate).getTime() >= newDate.getTime()) { //已经超时了的
                String id = AppUtils.getUUID();
                SuperviseHandleEntity superviseHandleEntity = new SuperviseHandleEntity();
                superviseHandleEntity.setId(id);
                superviseHandleEntity.setCreateDate(new Date());
                superviseHandleEntity.setCreateOper("1");
                superviseHandleEntity.setMatterId(assistList.get(i).getId());
                superviseHandleEntity.setMatterType("1");
                superviseHandleEntity.setAssignDate(assistList.get(i).getAssignDate());
                SysUserEntity user = sysUserService.getById(userAssignService.getByMatterId(assistList.get(i).getId()).getOperatorId());
                superviseHandleEntity.setOperatorId(user.getUserId().toString());
                superviseHandleEntity.setOperatorName(user.getUsername());
                superviseHandleEntity.setRequirement(assistList.get(i).getRequirement());
                superviseHandleService.save(superviseHandleEntity);

                NewsNoticeEntity noticeEntity = new NewsNoticeEntity();
                noticeEntity.setId(AppUtils.getUUID());
                noticeEntity.setCreateDate(new Date());
                noticeEntity.setCreateOper("1");
                noticeEntity.setMatterId(id);
                noticeEntity.setMatterType("2");  //督办的
                noticeEntity.setPushUserId(user.getUserId().toString());
                newsNoticeService.save(noticeEntity);
            }
        }
        List<ToDoImmediatelyEntity> toDoList = toDoImmediatelyService.queryIsAssign();
        for (int i = 0; i < toDoList.size(); i++) {
            Date assignDate = toDoList.get(i).getAssignDate();
            if (DateUtils.getAfterDay(assignDate).getTime() >= newDate.getTime()) { //已经超时了的
                String id = AppUtils.getUUID();
                SuperviseHandleEntity superviseHandleEntity = new SuperviseHandleEntity();
                superviseHandleEntity.setId(id);
                superviseHandleEntity.setCreateDate(new Date());
                superviseHandleEntity.setCreateOper("1");
                superviseHandleEntity.setMatterId(toDoList.get(i).getId());
                superviseHandleEntity.setMatterType("2");
                superviseHandleEntity.setAssignDate(toDoList.get(i).getAssignDate());
                SysUserEntity user = sysUserService.getById(userAssignService.getByMatterId(toDoList.get(i).getId()).getOperatorId());
                superviseHandleEntity.setOperatorId(user.getUserId().toString());
                superviseHandleEntity.setOperatorName(user.getUsername());
                superviseHandleEntity.setRequirement(toDoList.get(i).getLawsuitContent());
                superviseHandleService.save(superviseHandleEntity);

                NewsNoticeEntity noticeEntity = new NewsNoticeEntity();
                noticeEntity.setId(AppUtils.getUUID());
                noticeEntity.setCreateDate(new Date());
                noticeEntity.setCreateOper("1");
                noticeEntity.setMatterId(id);
                noticeEntity.setMatterType("2");  //督办的
                noticeEntity.setPushUserId(user.getUserId().toString());
                newsNoticeService.save(noticeEntity);
            }
        }
        System.err.println("结束查找24小时未办理完成的");
    }

    //检查竞价项目是否已经开始或者结束，修改相应状态
    //一分钟检查一次
    @Scheduled(fixedDelay = 60000)
    @Async
    public void checkBiddProject() {
        //已经开始
        List<BiddProjectEntity> startList = biddProjectService.list(new QueryWrapper<BiddProjectEntity>().eq("status", 0).le("start_time", new Date()));
        startList.forEach(e->{
            e.setStatus(1);
            biddProjectService.updateById(e);
            biddProjectService.sendSms(e);
        });
        log.error("----------更新已经开始的竞价项目---------------");
        //结束
        List<BiddProjectEntity> endList = biddProjectService.list(new QueryWrapper<BiddProjectEntity>().eq("status", 1).le("end_time", new Date()));
        endList.forEach(e -> {
            List<BiddEntity> biddList = biddService.list(new QueryWrapper<BiddEntity>().eq("project_id", e.getId()));
            if (biddList != null && biddList.size() > 0) {
                biddProjectService.update(new UpdateWrapper<BiddProjectEntity>().eq("id", e.getId()).set("status", 3));
            }else{
                biddProjectService.update(new UpdateWrapper<BiddProjectEntity>().eq("id", e.getId()).set("status", 5));
            }
        });
        log.error("--------------更新已结束的竞价项目------------");
    }

}
